package com.jy.blog.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jy.blog.model.Message;
import com.jy.blog.service.MessageService;
import com.jy.blog.service.impl.MessageServiceImpl;

/**
 * Servlet implementation class AddMessageServlet
 */
public class AddMessageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	 private MessageService service=new MessageServiceImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddMessageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		add(request,response);
	}
	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Message message=new Message();
		message.setMessContent(request.getParameter("content"));
		message.setMessTitle(request.getParameter("title"));
		message.setMessDate(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
		
		service.add(message);
		try{
			response.sendRedirect(request.getContextPath()+"/message.jsp");
		}catch(IOException e){
			e.printStackTrace();
		}
		
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
